<?php
class DB_MySQL{
	var $querycount = 0;

	function connect($servername, $dbusername, $dbpassword, $dbname, $usepconnect=0){
		
		if($usepconnect){
			if(!@mysql_pconnect($servername, $dbusername, $dbpassword)){
				$this->halt("Can not connect MySQL");
			}
		}else{
			if(!@mysql_connect($servername, $dbusername, $dbpassword)){
				$this->halt("Can not connect MySQL");
			}
		}
		if($this->version() > '4.1'){
			$db_charset = str_replace('-','','utf-8');
			mysql_query("SET NAMES $db_charset");
		}
		if($dbname){
			$this->select_db($dbname);
		}
	}
	function select_db($dbname){
		return mysql_select_db($dbname);
	}
	function query($sql){
		$query = mysql_query($sql);
		if(!$query)
		{
			$this->halt('MySQL Query Error', $sql);
		}
		$this->querycount++;
		return $query;
	}
	function query_bak($sql, $type = '') {
		$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
			'mysql_unbuffered_query' : 'mysql_query';
		if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
			$this->halt('MySQL Query Error', $sql);
		}
		$this->querynum++;
		return $query;
	}
	function getAll($sql, $cache = false){
	    $select_result = mysql_query($sql);
	    while( $record = mysql_fetch_array($select_result) ){
	        $records_arr[] = $record;
		}
		
	    return $records_arr;
	}
	
	function execute_sql($sql){
	    $select_result = mysql_query($sql) or die(mysql_error());
	    return  $select_result;
	}

	function fetch_array($query, $type=MYSQL_ASSOC){
		return mysql_fetch_array($query, $type);
	}
	function get_one_arr($sql, $cache = false){		
		$result = $this->query($sql);
		$record = $this->fetch_array($result);

		return $record;
	}
	function get_all_arr($sql, $cache = false){				
		$result = $this->query($sql);
		$i=0;
		$record = array();
		while($rs = $this->fetch_array($result)){
			$record[$i]=$rs;
			$i++;
		}
		
		return $record;
	}
	function fetch_row($query){
		$query = mysql_fetch_row($query);
		return $query;
	}
	function num_rows($query){
		$query = mysql_num_rows($query);
		return $query;
	}
	function result($num, $sql){
		$query = $this->query($sql);
		$record = $this->fetch_array($query, MYSQL_NUM);

		return $record[$num];
	}
	function free_result($query){
		$query = mysql_free_result($query);
		return $query;
	}
	function close(){
		return mysql_close();
	}
	function version(){
		$versions=explode('-',mysql_get_server_info(),2);
		$version=$versions['0'];
		return $version;
	}
	function errno(){
		return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
	}
	function error(){
		return (($this->link) ? mysql_error($this->link) : mysql_error());
	}
	function insert_id(){
		return mysql_insert_id();
	}
	function num_fields($query){
		return mysql_num_fields($query);
	}
	function affected_rows(){
		return mysql_affected_rows();
	}
	function halt($msg,$sql=''){
		exit("<pre>".$msg."\n\n{$sql}</pre>");
	}

	//插入数据
	public function inserttable($tablename, $insertsqlarr, $ReturnId=1, $replace = false, $silent=0) 
    {
        $insertkeysql = $insertvaluesql = $comma = '';
        foreach ($insertsqlarr as $insert_key => $insert_value) {
            $insertkeysql .= $comma.'`'.$insert_key.'`';
            $insertvaluesql .= $comma.'\''.$insert_value.'\'';
            $comma = ', ';
        }
        $method = $replace?'REPLACE':'INSERT';

        $this->query($method.' INTO '.$tablename.' ('.$insertkeysql.') VALUES ('.$insertvaluesql.')', $silent?' SILENT':'');
        
        if($ReturnId && !$replace) {
            return $this->insert_id();
        }
    }

    //更新数据
    public function updatetable($tablename, $setsqlarr, $wheresqlarr, $silent=0) 
    {
        $setsql = $comma = '';
        foreach ($setsqlarr as $set_key => $set_value) {
            $setsql .= $comma.'`'.$set_key.'`'.'=\''.$set_value.'\'';
            $comma = ', ';
        }
        $where = $comma = '';
        if(empty($wheresqlarr)) {
            $where = '1';
        } elseif(is_array($wheresqlarr)) {
            foreach ($wheresqlarr as $key => $value) {
                $where .= $comma.'`'.$key.'`'.'=\''.$value.'\'';
                $comma = ' AND ';
            }
        } else {
            $where = $wheresqlarr;
        }
        $rs = $this->query('UPDATE '.$tablename.' SET '.$setsql.' WHERE '.$where, $silent?'SILENT':'');
        return $rs;
    }
}
?>